package com.ruoyi.system.service.impl;

import java.util.List;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.frs.v2.FrsClient;
import com.huaweicloud.sdk.frs.v2.model.*;
import com.huaweicloud.sdk.frs.v2.region.FrsRegion;
import com.ruoyi.system.domain.FaceSetInfo;
import com.ruoyi.system.domain.dto.UpdateFaceByFaceId;
import com.ruoyi.system.mapper.FaceSetInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.FaceSetFaceMapper;
import com.ruoyi.system.domain.FaceSetFace;
import com.ruoyi.system.service.IFaceSetFaceService;
import org.springframework.web.multipart.MultipartFile;


/**
 * 系统人脸集Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-05
 */
@Service
public class FaceSetFaceServiceImpl implements IFaceSetFaceService 
{

    private static FrsClient client;

    static {

        ICredential auth = new BasicCredentials()
                .withAk("9LCWQGUFXTMAVTQQHXPU")
                .withSk("ixf7b2vxl7y6WA7Lwa7l9wOq8009ZJUtFMSrc2K8");

        client = FrsClient.newBuilder()
                .withCredential(auth)
                .withRegion(FrsRegion.valueOf("cn-north-4"))
                .build();
    }

    @Autowired
    private FaceSetFaceMapper faceSetFaceMapper;
    @Autowired
    private FaceSetInfoMapper faceSetInfoMapper;

    /**
     * 查询系统人脸集
     * 
     * @param id 系统人脸集主键
     * @return 系统人脸集
     */
    @Override
    public FaceSetFace selectFaceSetFaceById(Long id)
    {
        return faceSetFaceMapper.selectFaceSetFaceById(id);
    }

    /**
     * 查询系统人脸集列表
     * 
     * @param faceSetFace 系统人脸集
     * @return 系统人脸集
     */
    @Override
    public List<FaceSetFace> selectFaceSetFaceList(FaceSetFace faceSetFace)
    {
        return faceSetFaceMapper.selectFaceSetFaceList(faceSetFace);
    }

    /**
     * 新增系统人脸集
     * 
     * @param faceSetFace 系统人脸集
     * @return 结果
     */
    @Override
    public int insertFaceSetFace(FaceSetFace faceSetFace)
    {
        return faceSetFaceMapper.insertFaceSetFace(faceSetFace);
    }

    /**
     * 修改系统人脸集
     * 
     * @param faceSetFace 系统人脸集
     * @return 结果
     */
    @Override
    public int updateFaceSetFace(FaceSetFace faceSetFace)
    {
        return faceSetFaceMapper.updateFaceSetFace(faceSetFace);
    }

    /**
     * 批量删除系统人脸集
     * 
     * @param ids 需要删除的系统人脸集主键
     * @return 结果
     */
    @Override
    public int deleteFaceSetFaceByIds(Long[] ids)
    {
//        FaceSetFace face = faceSetFaceMapper.selectFaceSetFaceById(ids[0]);
//        DeleteFaceByFaceIdRequest request = new DeleteFaceByFaceIdRequest();
//        FaceSetInfo info = faceSetInfoMapper.selectFaceSetInfoByFaceSetId(face.getFaceSetId());
//        request.setFaceSetName(info.getFaceSetName());
//        request.setEnterpriseProjectId("06eaaa18a78025312f97c00abbb0c538");
//        request.setFaceId(face.getFaceId());
//        DeleteFaceByFaceIdResponse response = client.deleteFaceByFaceId(request);

        return faceSetFaceMapper.deleteFaceSetFaceByIds(ids);
    }

    /**
     * 删除系统人脸集信息
     * 
     * @param id 系统人脸集主键
     * @return 结果
     */
    @Override
    public int deleteFaceSetFaceById(Long id)
    {
        return faceSetFaceMapper.deleteFaceSetFaceById(id);
    }

    @Override
    public DeleteFaceByFaceIdResponse removeFace(String faceId, String faceSetName) {
        DeleteFaceByFaceIdRequest request = new DeleteFaceByFaceIdRequest();
        request.setFaceSetName(faceSetName);
        request.setEnterpriseProjectId("06eaaa18a78025312f97c00abbb0c538");
        request.setFaceId(faceId);
        DeleteFaceByFaceIdResponse response = client.deleteFaceByFaceId(request);
        return response;
    }

    @Override
    public UpdateFaceResponse updateFaceByFaceId(UpdateFaceByFaceId dto) {
        UpdateFaceRequest req = new UpdateFaceRequest();

        UpdateFaceReq body = new UpdateFaceReq();
        body.setFaceId(dto.getFaceId());
        req.setFaceSetName(dto.getFaceSetName());
        body.setExternalImageId(dto.getExternalImageId());
        req.setBody(body);
        UpdateFaceResponse response = client.updateFace(req);
        return response;
    }

    @Override
    public ShowFacesByFaceIdResponse showFacesByFaceId(String faceId, String faceSetName) {
        ShowFacesByFaceIdRequest request = new ShowFacesByFaceIdRequest();
        request.setFaceId(faceId);
        request.setFaceSetName(faceSetName);
        request.setEnterpriseProjectId("06eaaa18a78025312f97c00abbb0c538");
        ShowFacesByFaceIdResponse response = client.showFacesByFaceId(request);
        return response;
    }
}
